﻿using System;
using System.Collections.Generic;
using System.Linq;
using Simple.Data;

namespace Model.Course
{
    public class Task
    {
        protected string name;
        protected DateTime startTime;
        protected DateTime endTime;
        protected String content;
        protected Int32 id;
        protected Int32 courseId;

        protected Task(Int32 id, string name, DateTime startTime, DateTime endTime, 
                       String content, Int32 courseId)
        {
            this.id = id;
            this.name = name;
            this.startTime = startTime;
            this.endTime = endTime;
            this.content = content;
            this.courseId = courseId;
        }

        public static Task create(Course course, String name, DateTime startTime, DateTime endTime, String content)
        {
            var db = Database.Open();

            var task = db.Task.Insert(Name: name, Start: startTime, End: endTime,
                                         Content: content, Course: course.getId());

            return new Task(task.id, name, startTime, endTime, content, course.getId());
        }

        public static Task get(Int32 id)
        {
            var t = Database.Open().Task.Get(id);

            if (t != null)
                return new Task(t.id, t.name, t.start, t.end, t.content, t.course);

            return null;
        }

        public static Task[] getTasks(Course course)
        {
            var tasksRows = Database.Open().Task.FindAllByCourse(course.getId());
            int count = Database.Open().Task.GetCountByCourse(course.getId());

            Task[] tasks = new Task[count];

            int i = 0;
            foreach (var t in tasksRows)
                tasks[i++] = new Task(t.id, t.name, t.start, t.end, t.content, course.getId());

            return tasks;
        }

        public void delete()
        {
            Database.Open().Task.DeleteById(id);
        }

        public Int32 getId()
        {
            return id;
        }

        public string getName()
        {
            return name;
        }

        public DateTime getStartTime() 
        {
            if (startTime == null)
            {
                var task = Database.Open().Task.Get(id);
                startTime = task.StartTime;
            }

            return startTime;
        }

        public void setStartTime(DateTime value)
        {
            startTime = value;
            Database.Open().Task.UpdateByID(ID: id, StartTime: startTime);
        }

        public DateTime getEndTime()
        {
            if (endTime == null)
            {
                var task = Database.Open().Task.Get(id);
                endTime = task.EndTime;
            }

            return endTime;
        }

        public void setEndTime(DateTime value)
        {
            endTime = value;
            Database.Open().Task.UpdateByID(ID: id, EndTime: endTime);
        }

        public String getContent()
        {
            if (content == null)
            {
                var task = Database.Open().Task.Get(id);
                content = task.content;
            }

            return content;
        }

        public void setcontent(String value)
        {
            content = value;
            Database.Open().Task.UpdateByID(ID: id, Content: content);
        }

        public Attachment[] getAttachments()
        {
            return Attachment.getAttachments(Attachment.OwnerType.Task, id);
        }

        public void addAttachment(String fileName, String filePath)
        {
            Attachment a = Attachment.create(fileName, filePath);
            a.setOwnerId(Attachment.OwnerType.Task, id);
        }

        public void addAttachment(Attachment a)
        {
            a.setOwnerId(Attachment.OwnerType.Task, id);
        }
    }
}